package com.bocai.freeman.view.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseFragment;
import com.bocai.freeman.view.adapter.OnlineChatAdapter;
import com.bocai.freeman.view.custom.NoScrolGridView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 首页Frag
 */
public class HomeFrag extends BaseFragment {


    View view;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.ngv_home_chat)
    NoScrolGridView ngvHomeChat;
    @Bind(R.id.tl_home_charm)
    TabLayout tlHomeCharm;
    @Bind(R.id.vp_home_charm)
    ViewPager vpHomeCharm;
    @Bind(R.id.ngv_home_service)
    NoScrolGridView ngvHomeService;
    @Bind(R.id.tl_home_regal)
    TabLayout tlHomeRegal;
    @Bind(R.id.vp_home_regal)
    ViewPager vpHomeRegal;
    @Bind(R.id.tl_home_vip)
    TabLayout tlHomeVip;
    @Bind(R.id.vp_home_vip)
    ViewPager vpHomeVip;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        initToolbar();
        ngvHomeChat.setAdapter(new OnlineChatAdapter(getActivity(),10));
        ngvHomeService.setAdapter(new OnlineChatAdapter(getActivity(),5));
        vpHomeCharm.setAdapter(new CharmAdapter(getFragmentManager()));
        tlHomeCharm.setupWithViewPager(vpHomeCharm);
        vpHomeCharm.setOffscreenPageLimit(2);
        tlHomeCharm.setTabMode(TabLayout.MODE_FIXED);


        vpHomeRegal.setAdapter(new RegalAdapter(getFragmentManager()));
        tlHomeRegal.setupWithViewPager(vpHomeRegal);
        vpHomeRegal.setOffscreenPageLimit(2);
        tlHomeRegal.setTabMode(TabLayout.MODE_FIXED);


        vpHomeVip.setAdapter(new VIPAdapter(getFragmentManager()));
        tlHomeVip.setupWithViewPager(vpHomeVip);
        vpHomeVip.setOffscreenPageLimit(2);
        tlHomeVip.setTabMode(TabLayout.MODE_FIXED);
    }

    private void initToolbar() {
        title.setText("首页");
    }

    @Override
    public void setData(Object obj) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
    class CharmAdapter extends FragmentPagerAdapter {
        private final Fragment[] fragments = new Fragment[2];
        private final String [] titles= {getResources().getString(R.string.str_home_data_charm_value),getResources().getString(R.string.str_home_data_reputation_value)};
        int[] types={12,14};
        public CharmAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            Fragment fragment = fragments[position];
            if (fragment == null) {
                fragment = new CharmFragment();
                fragments[position] = fragment;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return titles[position];
        }
    }
    class RegalAdapter extends FragmentPagerAdapter {
        private final Fragment[] fragments = new Fragment[2];
        private final String [] titles= {getResources().getString(R.string.str_home_data_regals),getResources().getString(R.string.str_home_data_tuhao)};
        int[] types={12,14};
        public RegalAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            Fragment fragment = fragments[position];
            if (fragment == null) {
                fragment = new RegalFragment();
                fragments[position] = fragment;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return titles[position];
        }
    }
    class VIPAdapter extends FragmentPagerAdapter {
        private final Fragment[] fragments = new Fragment[2];
        private final String [] titles= {getResources().getString(R.string.str_home_data_new_vip),getResources().getString(R.string.str_home_data_new_user)};
        int[] types={12,14};
        public VIPAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            Fragment fragment = fragments[position];
            if (fragment == null) {
                fragment = new VIPFragment();
                fragments[position] = fragment;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return titles[position];
        }
    }
}
